package com.tanhua.server.controller;

import com.baomidou.mybatisplus.annotation.TableField;
import com.tanhua.autoconfig.template.AipFaceTemplate;
import com.tanhua.autoconfig.template.OssTemplate;
import com.tanhua.commons.utils.JwtUtils;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.UserInfoVo;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.service.TanhuaService;
import com.tanhua.server.service.UserInfoSerivce;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/users")
public class UsersController {
    @Autowired
    private UserInfoSerivce userInfoService;
    @Autowired
    private TanhuaService tanhuaService;

    /**
     * 查询用户资料
     */
    @GetMapping
    public ResponseEntity users(Long userID,@RequestHeader("Authorization") String token) {
        if(userID == null) {
            Claims claims = JwtUtils.getClaims(token);
            Integer id = (Integer) claims.get("id");
            userID = Long.valueOf(id);
        }
        UserInfoVo userInfo = userInfoService.findById(userID);
        log.info("userInfo = {}",userInfo);
        return ResponseEntity.ok(userInfo);
    }

    /**
     * 更新用户资料
     */
    @PutMapping
    public ResponseEntity updateUserInfo(@RequestBody UserInfo userInfo, @RequestHeader("Authorization") String token) {
        Claims claims = JwtUtils.getClaims(token);
        Integer id = (Integer) claims.get("id");
        userInfo.setId(Long.valueOf(id));
        userInfoService.update(userInfo);
        return ResponseEntity.ok(null);
    }

    @GetMapping("/friends/{code}")
    public ResponseEntity friends(@PathVariable("code") int code){
        /*
        PageResult pageResult = userInfoService.friends(code,UserHolder.getUserId());
        List<UserInfoVo> items = (List<UserInfoVo>) pageResult.getItems();
        for (UserInfoVo item : items) {
            System.out.println(item);
        }
        return ResponseEntity.ok(pageResult);
         */
        List<UserInfo> friends = userInfoService.friends(code,UserHolder.getUserId());
        /*for (UserInfoVo friend : friends) {
            System.out.println(friend.toString());
        }*/
        PageResult pr = new PageResult();
        pr.setItems(friends);
        //PageResult pr = userInfoService.friends(code, UserHolder.getUserId());

        return ResponseEntity.ok(pr);
    }

    @DeleteMapping("/like/{id}")
    public ResponseEntity<Void> notLikeUser(@PathVariable("id") Long likeUserId) {
        tanhuaService.delectLikeUser(likeUserId);
        return ResponseEntity.ok(null);
    }
}
